﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WestWorldRemake.Messaging;
using WestWorldRemake.States;

namespace WestWorldRemake.Characters
{
    public class Miner : BaseGameEntity
    {
        //the amount of gold a miner must have before he feels comfortable
        public readonly int ComfortLevel = 5;
        //the amount of nuggets a miner can carry
        public readonly int MaxNuggets = 3;
        //above this value a miner is thirsty
        public readonly int ThirstLevel = 5;
        //above this value a miner is sleepy
        public readonly int TirednessThreshold = 5;

        private StateMachine<Miner> m_pStateMachine;
        LocationType m_Location;
        //how many nuggets the miner has in his pockets
        private int m_iGoldCarried;
        private int m_iMoneyInBank;
        //the higher the value, the thirstier the miner
        private int m_iThirst;
        //the higher the value, the more tired the miner
        private int m_iFatigue;

        public LocationType Location
        {
            get
            {
                return m_Location;
            }
        }
        public void ChangeLocation(LocationType location)
        {
            m_Location = location;
        }

        public Miner(string name, ConsoleColor color) 
            : base (name, color)
        {
            m_iFatigue = 0;
            m_iGoldCarried = 0;
            m_iThirst = 0;
            m_iMoneyInBank = 0;
            m_Location = LocationType.Shack;
            m_pStateMachine = new StateMachine<Miner>(this, null, MinerOwned.GoHomeAndSleepTilRested.Instance);
        }
        public void AddToGoldCarried(int val)
        {
            m_iGoldCarried += val;
            if (m_iGoldCarried < 0)
                m_iGoldCarried = 0;
        }
        public int GetGoldCarried()
        {
            return m_iGoldCarried;
        }
        public void SetGoldCarred(int val)
        {
            m_iGoldCarried = val;
        }

        public bool PocketsFull()
        {
            return (m_iGoldCarried >= MaxNuggets);
        }

        public bool IsFatigued()
        {
            return (m_iFatigue >= TirednessThreshold);
        }
        public void DecreaseFatigue()
        {
            m_iFatigue--;
        }
        public void IncreaseFatigue()
        {
            m_iFatigue++;
        }

        public int Wealth()
        {
            return m_iMoneyInBank;
        }
        public void AddToWealth(int val)
        {
            m_iMoneyInBank += val;
        }
        public void SetWealth(int val)
        {
            m_iMoneyInBank = val;
        }
        
        public bool IsThirsty()
        {
            return (m_iThirst >= ThirstLevel);
        }
        public void BuyAndDrinkAWhiskey()
        {
            m_iThirst = 0;
            m_iMoneyInBank -= 2;
        }

        public StateMachine<Miner> StateMachine
        {
            get
            {
                return m_pStateMachine;
            }
        }
        public override void Update()
        {
            m_iThirst++;
            StateMachine.Update();
        }
        public override Iffy HandleMessage(Telegram message)
        {
            return m_pStateMachine.HandleMessage(message);
        }
    }
}
